class Record < ActiveRecord::Base
  belongs_to :worker
  belongs_to :track

  def self.findRecords name,begin_time,end_time,track_id,start,limit
    conditionsStr = '(worker_id in (select id from workers where name like :name))'
    conditionsHash = Hash.new

    conditionsHash[:name] = '%'+name+'%'
    if begin_time != ''
      conditionsStr += ' AND check_time >= :begin_time'
      conditionsHash[:begin_time] = Time.parse(begin_time)
    end
    if end_time != ''
      conditionsStr += ' AND check_time <= :end_time'
      conditionsHash[:end_time] = Time.parse(end_time)
    end
    if track_id != ''
      conditionsStr += ' AND track_id = :track_id'
      conditionsHash[:track_id] = track_id.to_i
    end

    find(:all,:conditions => [conditionsStr,conditionsHash],:offset => start, :limit => limit,:order => 'check_time DESC')
  end

  def self.countRecords name,begin_time,end_time,track_id
    conditionsStr = '(worker_id in (select id from workers where name like :name))'
    conditionsHash = Hash.new

    conditionsHash[:name] = '%'+name+'%'
    if begin_time != ''
      conditionsStr += ' AND check_time >= :begin_time'
      conditionsHash[:begin_time] = Time.parse(begin_time)
    end
    if end_time != ''
      conditionsStr += ' AND check_time <= :end_time'
      conditionsHash[:end_time] = Time.parse(end_time)
    end
    if track_id != ''
      conditionsStr += ' AND track_id = :track_id'
      conditionsHash[:track_id] = track_id.to_i
    end
    puts conditionsStr
    count(:all,:conditions => [conditionsStr,conditionsHash])
  end
end
